/*
提交链接：https://leetcode.cn/problems/magnetic-force-between-two-balls/description/
1552. 两球之间的磁力
赖德檀 2024/9/17
*/

class Solution {
public:
int test(vector<int>& position,int k)
{
    int ans=position[0];
    int sum=1;
    for(int i=1;i<position.size();i++)
    {
        if(position[i]-ans>=k)
        {
            sum++;
            ans=position[i];
        }
    }
    return sum;
}


    int maxDistance(vector<int>& position, int m) {
        int n=position.size();
        sort(position.begin(),position.end());
        int l=1,r=position[n-1];
        while(l<r)
        {
            int mid=l+(r-l+1)/2;
            if(test(position,mid)<m)
            r=mid-1;
            else
            l=mid;
        }
        return l;
    }
};